package net.sx.minemodule.mvp.presenters;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.homegoods.area.model.SubModelList;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BasePresenter;

import net.sx.minemodule.MineApiUtils;
import net.sx.minemodule.mvp.bean.GoodsExchangeModel;
import net.sx.minemodule.mvp.model.ConvertListModel;
import net.sx.minemodule.mvp.model.MineMedalInfoModel;
import net.sx.minemodule.mvp.views.MineMedalView;

import org.json.JSONObject;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

public class MineMedalPresenter extends BasePresenter<MineMedalView> {
    private static final String TAG = "MineMedalPresenterTag";
    private MultiTypeAdapter adapter = new MultiTypeAdapter();  //初始化adapter

    public MineMedalPresenter(MineMedalView view) {
        super(view);
        addData();
    }

    /**
     * 获取个人奖牌信息
     */
    public void getMedalData() {
        MineApiUtils.getMedalInfo(medalInfoModel -> mView.addMedalData(medalInfoModel),
                throwable -> Log.d(TAG, "accept: throwable == " + throwable.toString()));
    }

    /**
     * 获取兑换商品列表
     */
    public void getConvertList() {
        //获取兑换商品列表请求接口
        MineApiUtils.getConvertList(1, 10, convertListModel -> {
            Log.d(TAG, "accept: convertList == " + JSON.toJSONString(convertListModel));
            List<ConvertListModel> convertListModels = JSON.parseArray(convertListModel.getItems(), ConvertListModel.class);
            for (ConvertListModel model : convertListModels) {
                adapter.addItem(model.createItem(adapter));     //设置adapter数据
            }
            adapter.notifyDataSetChanged();
        }, throwable -> Log.d(TAG, "accept: throwable == " + throwable.toString()));
    }

    /**
     * 获取adapter
     * @return
     */
    public MultiTypeAdapter getAdapter() {
        return adapter;
    }

    private void addData() {
        addSubscription(Observable.range(0, 3)
        .map(integer -> {
            GoodsExchangeModel exchangeModel = new GoodsExchangeModel();
            exchangeModel.setDepleteNum("消耗奖牌：2个");
            exchangeModel.setGoodsName("北欧设计落地灯");
            return exchangeModel;
        })
        .toList()
        .subscribe());
    }
}
